home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Dev / misc / temgen.lha / Temgen / tg-0.11 / stack.c < prev    next >
C/C++ Source or Header  |  2002-12-18  |  1KB  |  55 lines

  1. #include "alloc.h"
  2. #include "generator.h"
  3. #include "list.h"
  4. #include "stack.h"
  5. #include "sysdefs.h"
  6. #include "omani.h"
  7.  
  8. static int Stack = -1;
  9. static int last_alloc = 0;
  10. static struct list_head notused;      /* not used slots */
  11.  
  12. struct sitem {
  13.     struct list_head list;
  14.     int id;
  15. };
  16.  
  17. void stinit( int stack )
  18. {
  19.     Stack = stack;
  20.     INIT_LIST_HEAD( ¬used );
  21. }
  22.  
  23. int stalloc( void )
  24. {
  25.     int res;
  26.     struct sitem *it;
  27.     
  28.     if ( Stack <= 0 ) return -1;
  29.     
  30.     /* look for recyclable entry */
  31.     if ( notused.next != ¬used ) {
  32.         it = list_entry( notused.next, struct sitem, list );
  33.         res = it->id;
  34.         list_del( &it->list );
  35.         FREE( it );
  36.         return res;
  37.     }
  38.     
  39.     res = ++last_alloc;
  40.     return ob_item( Stack, res );
  41. }
  42.  
  43. void stfree( int obj )
  44. {
  45.     struct sitem *it;
  46.     
  47.     if ( Stack <= 0 ) return;
  48.     
  49.     ob_set( obj, 'i', 0 );
  50.     it = (struct sitem*)MALLOC( sizeof(*it) );
  51.     if ( !it ) fatal( "Memory allocation error in 'stfree'" );
  52.     it->id = obj;
  53.     list_add( &it->list, ¬used );
  54. }
  55.